package com.edu.controller;


import com.JwtUtils;
import com.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.client.addComment;
import com.edu.entity.Comment;
import com.edu.service.CommentService;
import com.educenter.entity.UcenterMember;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/comment")
public class CommentController {

	@Autowired
	private CommentService commentService;

	@Autowired
	private addComment addComment;

	//评论分页查询
	@GetMapping("/pageComment/{current}/{limit}")
	public R pageComment(@PathVariable long current, @PathVariable long limit){
		Page<Comment> page = new Page<>(current, limit);

		commentService.page(page,null);
		long total = page.getTotal();
		List<Comment> records = page.getRecords();


		return R.ok().data("total",total).data("rows",records);
	}



	//根据课程id查询评论列表
	@ApiOperation(value = "评论分页列表")
	@PostMapping("/commentListByCourseId/{page}/{limit}")
	public R commentListByCourseId(
			@ApiParam(name = "page", value = "当前页码", required = true)
			@PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true)
			@PathVariable Long limit,

			@ApiParam(name = "courseId", value = "查询对象", required = false)
			@RequestBody String courseId) {

		Page<Comment> pageParam = new Page<>(page, limit);

		QueryWrapper<Comment> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id",courseId);

		commentService.page(pageParam,wrapper);
		List<Comment> commentList = pageParam.getRecords();

		Map<String, Object> map = new HashMap<>();
		map.put("items", commentList);
		map.put("current", pageParam.getCurrent());
		map.put("pages", pageParam.getPages());
		map.put("size", pageParam.getSize());
		map.put("total", pageParam.getTotal());
		map.put("hasNext", pageParam.hasNext());
		map.put("hasPrevious", pageParam.hasPrevious());
		return R.ok().data(map);
	}


	//添加评论
	@PostMapping("/addComment")
	public R addComment(@RequestBody Comment comment, HttpServletRequest request){
		String memberId = JwtUtils.getMemberIdByJwtToken(request);
		if (StringUtils.isEmpty(memberId)){
			return R.error().code(20001).message("请登录");
		}

		comment.setMemberId(memberId);
		UcenterMember member = addComment.getMemberById(memberId);

		comment.setNickname(member.getNickname());
		comment.setAvatar(member.getAvatar());

		commentService.save(comment);

		return R.ok();
//		R memberInfo = addComment.getMemberInfo(request);
//		UcenterMember userInfo = (UcenterMember) memberInfo.getData().get("userInfo");
//		String id = userInfo.getId();
//		String nickname = userInfo.getNickname();
//		String avatar = userInfo.getAvatar();
//		System.out.println(id);
//
//		comment.setMemberId(id);
//		comment.setNickname(nickname);
//		comment.setAvatar(avatar);
//		commentService.save(comment);
//		return R.ok();

	}

}
